#pragma once

#include "Geometry.h"
#include <stdio.h>
#include <vector>

class QuadTree
{
public:
	class Node
	{
	public:
		Node() {}
		Node(const Vector2& position, int index) : position_(position), index_(index) {}
		Vector2 position_;
		int index_;
	};

	class AABB
	{
	public:
		AABB(const Vector2& min, const Vector2& max) : min_(min), max_(max) {}
		AABB(float minX, float maxX, float minY, float maxY) : min_(minX, minY), max_(maxX, maxY) {}
		bool containsPoint(const Vector2& p) const;
		bool intersectsAABB(const AABB& other) const;

		Vector2 min_;
		Vector2 max_;
	};

	QuadTree(const AABB& boundary) : boundary_(boundary), numPoints_(0), numTotalPoints_(0), northWest_(NULL), northEast_(NULL), southWest_(NULL), southEast_(NULL) {}
	virtual ~QuadTree();

	bool insert(const Node& p);
	bool subdivide();
	bool queryRange(const AABB& range, std::vector<int>& indices) const;

protected:
	static const int QT_NODE_CAPACITY = 4;

	AABB boundary_;

	Node points_[QT_NODE_CAPACITY];
	int numPoints_;
	int numTotalPoints_;

	QuadTree* northWest_;
	QuadTree* northEast_;
	QuadTree* southWest_;
	QuadTree* southEast_;

};